#include <iostream>
#include <windows.h>
#include <psapi.h>
#include <tchar.h>
#include <stdio.h>
#include <ShellApi.h>
#include <wincon.h>

using namespace std;

const char* GameLocation;
const char* gawcf_version;
const char* version;
int gameRunning = 0;
int menu;
uint32_t TimerId_AoE2;
uint32_t TimerId_AoE2E;
uint32_t TimerId_HODWin;

bool isRunning(string pName)
{
	unsigned long aProcesses[1024], cbNeeded, cProcesses;
	if(!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))

	cProcesses = cbNeeded / sizeof(unsigned long);
	for(unsigned int i = 0; i < cProcesses; i++)
	{
		if(aProcesses[i] == 0)
			continue;

		HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, aProcesses[i]);
		char buffer[50];
		GetModuleBaseNameA(hProcess, 0, buffer, 50);
		CloseHandle(hProcess);
		if(pName == string(buffer))
			return true;
	}
	return false;
}

int killProcess(const char *szToTerminate)
{

	BOOL endtask = false;
	BOOL bResult;
	DWORD aiPID[1000],iCb=1000,iNumProc;
	DWORD iCbneeded,i;
	char szName[MAX_PATH],szToTermUpper[MAX_PATH];
	HANDLE hProc;
	HINSTANCE hInstLib;
	int iLen,iLenP,indx;
	HMODULE hMod;
	iLenP=strlen(szToTerminate);
	for(indx=0;indx<iLenP;indx++)
	szToTermUpper[indx]=toupper(szToTerminate[indx]);
	szToTermUpper[iLenP]=0;
	BOOL (WINAPI *lpfEnumProcesses)( DWORD *, DWORD cb, DWORD * );
	BOOL (WINAPI *lpfEnumProcessModules)( HANDLE, HMODULE *,DWORD, LPDWORD );
	DWORD (WINAPI *lpfGetModuleBaseName)( HANDLE, HMODULE,LPTSTR, DWORD );
	hInstLib = LoadLibraryA("PSAPI.DLL");
	lpfEnumProcesses = (BOOL(WINAPI *)(DWORD *,DWORD,DWORD*))GetProcAddress( hInstLib, "EnumProcesses" );
	lpfEnumProcessModules = (BOOL(WINAPI *)(HANDLE, HMODULE *,DWORD, LPDWORD)) GetProcAddress( hInstLib,"EnumProcessModules" );
	lpfGetModuleBaseName =(DWORD (WINAPI *)(HANDLE, HMODULE, LPTSTR, DWORD )) GetProcAddress( hInstLib,"GetModuleBaseNameA" );
	bResult=lpfEnumProcesses(aiPID,iCb,&iCbneeded);
	iNumProc=iCbneeded/sizeof(DWORD);
	for(i=0;i<iNumProc;i++){
	strcpy(szName,"Unknown");
	hProc=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_VM_READ,FALSE,aiPID[i]);
	if(hProc){
	if(lpfEnumProcessModules(hProc,&hMod,sizeof(hMod),&iCbneeded) ){
	iLen=lpfGetModuleBaseName(hProc,hMod,szName,MAX_PATH);}}
	CloseHandle(hProc);
	#ifdef BORLANDC
				if(strcmp(strupr(szName),szToTermUpper)==0)
	#else
				if(strcmp(_strupr(szName),szToTermUpper)==0)
	#endif

				{hProc=OpenProcess(PROCESS_TERMINATE,FALSE,aiPID[i]);
	if(hProc)
	{
		(TerminateProcess(hProc,1));
		endtask = true;
	}
	}
	}
	if(endtask) return(1);
	return(0);
}

void KillExplorer()
{
    killProcess("explorer.exe");
}

VOID CALLBACK CBTimerId_AoE2(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
    if(isRunning("empires2.exe"))
    {
        printf("Found the game.. Killing explorer..");
        KillExplorer();
        printf("Killing the timer to reduce Memory Consume...");
        KillTimer(NULL, TimerId_AoE2);
        printf("Ready to Play!");
    }
}

VOID CALLBACK CBTimerId_AoE2E(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
    if(isRunning("age2_x1.exe"))
    {
        printf("Found the game.. Killing explorer..");
        KillExplorer();
        printf("Killing the timer to reduce Memory Consume...");
        KillTimer(NULL, TimerId_AoE2E);
        printf("Ready to Play!");
    }
}

VOID CALLBACK CBTimerId_HODWin(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
    if(isRunning("HODWin32.exe"))
    {
        printf("Found the game.. Killing explorer..");
        KillExplorer();
        printf("Killing the timer to reduce Memory Consume...");
        KillTimer(NULL, TimerId_HODWin);
        printf("Ready to Play!");
    }
}


int main()
{
    gawcf_version = "0.1c CLOSED-BETA";
    version = "CLOSED-BETA";
    SetConsoleTitle("GAWCF: Colorfix 0.1c CLOSED-BETA");
    printf("The Gamers-Abode Colorfix %s Is Now Running Properly.\n", gawcf_version);
    printf("Is a %s only for GA Forums members Just press any key to run the loop.\n", version);
    system("pause");
    printf("1. AoE2 (No Expansion)");
    printf("2. AoE2 (Expansion)");
    printf("3. Heart of Darkness");
    scanf("%d", &menu);

    switch(menu)
    {
        case 1:
        TimerId_AoE2 = SetTimer(NULL, 1, 500, &CBTimerId_AoE2);
        break;

        case 2:
        TimerId_AoE2E = SetTimer(NULL, 1, 500, &CBTimerId_AoE2E);
        break;

        case 3:
        TimerId_HODWin = SetTimer(NULL, 1, 500, &CBTimerId_HODWin);
        break;

        default: printf("CRITICAL ERROR: U cannot Go without selecting a game"); break; exit(0);
    }
    return 0;
}
